package cc.htdf.gwc.forecast.service;

import cc.htdf.gwc.forecast.domain.response.ResponseCode;
import cc.htdf.gwc.forecast.repository.StationDataRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import whisper.common.utils.DateUtils;

import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by JT on 2017/10/10.
 */
@Service
public class StationDataServiceImpl implements StationDataService {

    @Autowired
    private StationDataRepository stationDataRepository;


    /**
     * 根据站号、要素、发布时次、开始预报时间、结束预报时间查询数据
     *
     * @param stations         ：站号
     * @param elements         ：要素
     * @param modelRunDateTime ：发布时次
     * @param validTimes       : 预报时间跨度 如：96小时
     * @return
     */
    @Override
    public List<Map<String, Object>> findChengDuStatioinForecastDataByStationsAndModelAndValidTime(String stations, String elements, String modelRunDateTime, String validTimes) {
        List<String> stationList = Arrays.asList(stations.split(","));
        String startValidTime = "", endValidTime = "";
        try {
            startValidTime = DateUtils.format(modelRunDateTime, "yyyyMMddHH", "yyyy-MM-dd HH:mm:ss");
            endValidTime = DateUtils.addHour(modelRunDateTime, Integer.valueOf(validTimes), "yyyyMMddHH", "yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            throw new whisper.common.exception.format.ParseException(ResponseCode.STATUS_ERROR, "时间格式转换出错！");
        }
        return stationDataRepository.findChengDuStatioinForecastDataByStationsAndModelAndValidTime(stationList, modelRunDateTime, startValidTime, endValidTime, elements);
    }

    /**
     * 根据站号、要素、预报时次查询预报数据
     *
     * @param stations  ：站号
     * @param elements  ：要素
     * @param validDate ：预报时间
     * @return
     */
    @Override
    public List<Map<String, Object>> findChengDuStationForecastDataByStationsAndValidDate(String stations, String elements, String validDate) {
        List<String> stationList = Arrays.asList(stations.split(","));
        return stationDataRepository.findChengDuStatioinForecastDataByStationsAndValidTime(stationList, validDate, elements)
                .stream().filter(map -> {
                    String str = map.get("MODELRUNDATETIME").toString();
                    return !StringUtils.isEmpty(str) && StringUtils.contains(str, " 12:");
                }).collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> findChengDuData(String stations, String elements, String modelRunDateTime, String validTimes) {
        List<String> stationList = Arrays.asList(stations.split(","));
        String startValidTime = "", endValidTime = "";
        try {
            startValidTime = modelRunDateTime;
            endValidTime = DateUtils.addHour(modelRunDateTime, Integer.valueOf(validTimes), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss");
            modelRunDateTime = modelRunDateTime.substring(0,13)+"%";
        } catch (ParseException e) {
            throw new whisper.common.exception.format.ParseException(ResponseCode.STATUS_ERROR, "时间格式转换出错！");
        }
        return stationDataRepository.findChengDuData(stationList, modelRunDateTime, startValidTime, endValidTime, elements);
    }

    @Override
    public List<Map<String, Object>> findChengDuData(String stations, String elements, String modelRunDateTime, String startValidTime, String endValidTime) {
        List<String> stationList = Arrays.asList(stations.split(","));
        try {
            modelRunDateTime = DateUtils.format(modelRunDateTime, "yyyyMMddhh", "yyyy-MM-dd HH%");
            startValidTime = DateUtils.format(startValidTime, "yyyyMMddHH", "yyyy-MM-dd HH:mm:ss");
            endValidTime = DateUtils.format(endValidTime, "yyyyMMddHH", "yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            throw new whisper.common.exception.format.ParseException(ResponseCode.STATUS_ERROR, "时间格式转换出错！");
        }
        return stationDataRepository.findChengDuData(stationList, modelRunDateTime, startValidTime, endValidTime, elements);
    }

    /**
     * 根据台站、要素、发布小时，预报时次查询数据
     *
     * @param station   ：台站号
     * @param elements  ：要素
     * @param modelhour : 发布小时
     * @param times     ： 预报时次， 前多少小时
     * @return
     */
    @Override
    public List<Map<String, Object>> findChengDuDataByStationAndModelHourAndTimes(String station, String elements, String modelhour, String times) {
        return stationDataRepository.findChengDuDataByStationAndModelHourAndTimes(station, elements, modelhour, times);
    }


}
